# @FileName  : work.py
# @Time      : 2021/9/1 0014  18:15
# @Author    : LuZhaoHui
# @Software  : PyCharm

from core.msql import *


def procPrintInfo(printMsg):
    info = printMsg.split(';')
    # 网关
    n1 = 0
    l1 = []
    # 站点
    n2 = 0
    l2 = []
    # 类型
    type = -1
    nums = 0
    # state
    state = ''
    # light
    light = 0
    # window
    w = 0
    h = 0
    # 版本
    ver = ''
    if len(info) > 0:
        for s in info:
            if 'gateWay=' == s[0:8]:
                n1 = int(s[8:])
                if n1 > 0:
                    l1 = [0] * n1
            if 'nodeLen=' == s[0:8]:
                n2 = int(s[8:])
                if n2 > 0:
                    l2 = [0] * n2
            if 'curDev=' == s[0:7] and n1 > 0 and n2 > 0:
                ids = s[7:].split(',')
                for i, id in enumerate(ids):
                    if i < n1:
                        l1[i] = int(id)
                    else:
                        if (i - n1) < n2:
                            l2[i - n1] = int(id)
            if 'canType=' == s[0:8]:
                type = int(s[8:])
            if 'workCount=' == s[0:10]:
                nums = int(s[10:])
            if 'canLocate=' == s[0:10]:
                state = s[10:]
            if 'light=' == s[0:6]:
                light = int(s[6:])
            if 'window=' == s[0:7]:
                wins = s[7:].split(',')
                w = int(wins[0])
                h = int(wins[1])
            if 'version=' == s[0:8]:
                ver = s[8:]
        return type, [l1, l2], nums, state, light, w, h, ver


# 打印报告类
class photoPrintReport():
    def __init__(self, msql=None):
        self.initVar()
        self.mysql = msql

    def setMysql(self, msql):
        self.mysql = msql

    # 初始化打印报告
    def initVar(self):
        self.PrintReport = {
            'CanPhotoFullStep1': 0,  # 卷片电机固定正文全幅步长
            'CanPhotoHalfStep1': 0,  # 卷片电机固定正文半幅步长
            'CanPhotoSpaceStep1': 0,  # 卷片电机固定正文间隔步长
            'CanPhotoFullStep2': 0,  # 卷片电机固定标版全幅步长
            'CanPhotoHalfStep2': 0,  # 卷片电机固定标版半幅步长
            'CanPhotoSpaceStep2': 0,  # 卷片电机固定标版间隔步长
            'ScreenStop': 0,  # 卷片电机停留时间 - ms
            'ScreenExposure1': 0,  # 正文显示曝光时间 - ms
            'ScreenExposure2': 0,  # 标版显示曝光时间 - ms
            'ScreenBlack': 0,  # 显示黑屏时间 - ms
            'CanPhotoRate': 0,  # 卷片电机固定速率
            'CanBeginStep': 0,  # 片头步长
            'CanEndStep': 0,  # 片头步长
            'CanBeginStepSpace': 0,  # 片头步长空拍
            'CanEndStepSpace': 0,  # 片头步长空拍
            'canMainStepSpace1': 0,  # 片头正文标版空拍
            'canMainStepSpace2': 0,  # 片尾正文标版空拍
            'screenDirection': 0,  # 卷片方向
            'CanSurveyEncode1000': 0,  # 卷片计量编码步长
            'CanSurveyPhotoNums': 0,  # 卷片计量拍数
            'CanStepToLength': 0,  # 卷片换算电机系数
            'CanEncodeToLength': 0,  # 卷片换算编码系数
            'CanEnCode100List': '',  # 卷片编码100步长列表
            'TotalStep': 0,  # 卷片总步长
            'TotalEncodeStep': 0,  # 卷片总编码步长
            'TotalSec': 0,  # 卷片总时间 - 秒
            'PhotoTotalLength': 0,  # 胶片总长度 - mm
            'PhotoUseLength': 0,  # 胶片使用长度 - mm
            'PhotoLeftLength': 0,  # 胶片剩余长度 - mm
            'PhotoTotalNums': 0,  # 胶片总拍数
            'PhotoImageNums': 0,  # 胶片有效拍数
            'PhotoType': '',  # 胶片类型 - 16 / 35 /3x
            'BeginDateTime': 0,  # 开始时间
            'EndDateTime': 0,  # 结束时间
            'WorkName': '',  # 项目名称
            'WorkId': 0,  # 项目ID
            'PrintName': '',  # 打印服务器名称
            'PrintDev': '',  # 打印服务器设备
            'PhotoModel': '',  # 胶片型号
            'Op': '',  # 操作员
            'SoftVer': '',  # 软件版本
            'Score': 0,  # 得分
            'Remark': ''  # 备注
        }
        return self.PrintReport

    def saveLog(self, printSsh=None):
        name = self.PrintReport['WorkName']
        id = self.PrintReport['WorkId']
        ip = self.PrintReport['PrintName'].split('-').pop()
        try:
            if printSsh == None:
                saveJson(self.PrintReport, log=name)
            else:
                isM = False
                if printSsh.sshConnect() == 0:
                    s = printSsh.putWorkPrintJson(name, id, self.PrintReport)
                    if s != '':
                        printSsh.workLogList[name][0] += 1
                        printSsh.workLogList[name][1] = nowDateTimeStr()
                        isM = True
                    printSsh.sshClose()
                if isM:
                    printSsh.putWorkLogList()
            # 写Mysql->photoLog
            if self.mysql != None:
                # addPhotoLog(self, work, photoId, beginTime, endTime, photoType, report):
                self.mysql.addPhotoLogEx(ip, name, id,
                                         self.PrintReport['BeginDateTime'],
                                         self.PrintReport['EndDateTime'],
                                         self.PrintReport['PhotoType'],
                                         json.dumps(self.PrintReport, ensure_ascii=False)
                                         )
        except Exception as e:
            saveLog('print:[%s->%s] save log err [%s]' % (name, id, str(e)), log='error')

    def setSys(self, op, ver):
        self.PrintReport['Op'] = op
        self.PrintReport['SoftVer'] = ver

    def setWork(self, workName):
        self.PrintReport['WorkName'] = workName
        id = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        self.PrintReport['WorkId'] = id
        return id

    def setPrint(self, printName, printMsg):
        self.PrintReport['PrintName'] = printName
        self.PrintReport['PrintDev'] = printMsg

    def setConf(self, varList):
        if len(varList) < 23:
            return
        self.PrintReport['CanPhotoFullStep1'] = int(varList[0])
        self.PrintReport['CanPhotoHalfStep1'] = int(varList[1])
        self.PrintReport['CanPhotoSpaceStep1'] = int(varList[2])
        self.PrintReport['CanPhotoFullStep2'] = int(varList[3])
        self.PrintReport['CanPhotoHalfStep2'] = int(varList[4])
        self.PrintReport['CanPhotoSpaceStep2'] = int(varList[5])
        self.PrintReport['ScreenStop'] = int(varList[6])
        self.PrintReport['ScreenExposure1'] = int(varList[7])
        self.PrintReport['ScreenExposure2'] = int(varList[8])
        self.PrintReport['ScreenBlack'] = int(varList[9])
        self.PrintReport['CanPhotoRate'] = int(varList[10])
        self.PrintReport['CanBeginStep'] = int(varList[11])
        self.PrintReport['CanEndStep'] = int(varList[12])
        self.PrintReport['CanBeginStepSpace'] = int(varList[13])
        self.PrintReport['CanEndStepSpace'] = int(varList[14])
        self.PrintReport['canMainStepSpace1'] = int(varList[15])
        self.PrintReport['canMainStepSpace2'] = int(varList[16])
        self.PrintReport['screenDirection'] = int(varList[17])
        self.PrintReport['CanSurveyEncode1000'] = int(varList[18])
        self.PrintReport['CanSurveyPhotoNums'] = int(varList[19])
        self.PrintReport['CanStepToLength'] = int(varList[20])
        self.PrintReport['CanEncodeToLength'] = int(varList[21])
        if len(varList) >= 24:
            self.PrintReport['ScreenExposureLoopTime'] = int(varList[23])
            self.PrintReport['ScreenExposureLoopNums'] = int(varList[24])

    def setBegin(self, totalStep, totalSec, totalLength, totalNums, imageNums, photoType):
        self.PrintReport['TotalStep'] = totalStep
        self.PrintReport['TotalSec'] = totalSec
        self.PrintReport['PhotoTotalLength'] = totalLength
        self.PrintReport['PhotoTotalNums'] = totalNums
        self.PrintReport['PhotoImageNums'] = imageNums
        self.PrintReport['PhotoType'] = photoType
        self.PrintReport['BeginDateTime'] = nowDateTimeInt()

    def setEnd(self, useLength, leftLength, totalEncode):
        self.PrintReport['TotalEncodeStep'] = totalEncode
        self.PrintReport['PhotoUseLength'] = useLength
        self.PrintReport['PhotoLeftLength'] = leftLength
        self.PrintReport['EndDateTime'] = nowDateTimeInt()


# 冲洗报告类
class photoDevelopRepoer():
    def __init__(self):
        pass

    def initVar(self):
        pass
